/**
 * 平台检测工具函数
 */

/**
 * 检测是否为移动端设备
 * @returns boolean
 */
export function isMobileDevice(): boolean {
  if (typeof window === 'undefined') {
    return false;
  }

  const userAgent = navigator.userAgent.toLowerCase();
  
  // 检测移动设备用户代理
  const mobileUserAgents = [
    'mobile', 'android', 'iphone', 'ipad', 'phone',
    'blackberry', 'opera mini', 'iemobile', 'wpdesktop',
    'fennec', 'kindle', 'playbook', 'tablet'
  ];

  // 检测特殊内置浏览器
  const specialBrowsers = [
    'micromessenger', // 微信
    'weibo',          // 微博
    'qq'              // QQ
  ];

  const isMobileUserAgent = 
    mobileUserAgents.some(agent => userAgent.includes(agent)) ||
    specialBrowsers.some(browser => userAgent.includes(browser));

  const isMobileScreen = window.innerWidth <= 768;

  return isMobileUserAgent || isMobileScreen;
}

/**
 * 检测是否为微信内置浏览器
 * @returns boolean
 */
export function isWeChatBrowser(): boolean {
  if (typeof window === 'undefined') {
    return false;
  }
  
  return /micromessenger/i.test(navigator.userAgent);
}

/**
 * 检测是否为QQ内置浏览器
 * @returns boolean
 */
export function isQQBrowser(): boolean {
  if (typeof window === 'undefined') {
    return false;
  }
  
  return /qq/i.test(navigator.userAgent);
}

/**
 * 检测是否为微博内置浏览器
 * @returns boolean
 */
export function isWeiboBrowser(): boolean {
  if (typeof window === 'undefined') {
    return false;
  }
  
  return /weibo/i.test(navigator.userAgent);
}

/**
 * 获取设备类型
 * @returns 'mobile' | 'tablet' | 'desktop'
 */
export function getDeviceType(): 'mobile' | 'tablet' | 'desktop' {
  if (typeof window === 'undefined') {
    return 'desktop';
  }

  const width = window.innerWidth;
  
  if (width <= 480) {
    return 'mobile';
  } else if (width <= 768) {
    return 'tablet';
  } else {
    return 'desktop';
  }
}

/**
 * 检测操作系统
 * @returns 'ios' | 'android' | 'windows' | 'macos' | 'linux' | 'unknown'
 */
export function getOperatingSystem(): 'ios' | 'android' | 'windows' | 'macos' | 'linux' | 'unknown' {
  if (typeof window === 'undefined') {
    return 'unknown';
  }

  const userAgent = navigator.userAgent.toLowerCase();
  
  if (/iphone|ipad|ipod/.test(userAgent)) {
    return 'ios';
  } else if (/android/.test(userAgent)) {
    return 'android';
  } else if (/windows/.test(userAgent)) {
    return 'windows';
  } else if (/macintosh|mac os x/.test(userAgent)) {
    return 'macos';
  } else if (/linux/.test(userAgent)) {
    return 'linux';
  } else {
    return 'unknown';
  }
}

/**
 * 检测浏览器类型
 * @returns string
 */
export function getBrowserType(): string {
  if (typeof window === 'undefined') {
    return 'unknown';
  }

  const userAgent = navigator.userAgent.toLowerCase();
  
  if (isWeChatBrowser()) {
    return 'wechat';
  } else if (isQQBrowser()) {
    return 'qq';
  } else if (isWeiboBrowser()) {
    return 'weibo';
  } else if (/chrome/.test(userAgent)) {
    return 'chrome';
  } else if (/firefox/.test(userAgent)) {
    return 'firefox';
  } else if (/safari/.test(userAgent)) {
    return 'safari';
  } else if (/edge/.test(userAgent)) {
    return 'edge';
  } else {
    return 'unknown';
  }
}

/**
 * 检测是否支持触摸
 * @returns boolean
 */
export function isTouchDevice(): boolean {
  if (typeof window === 'undefined') {
    return false;
  }

  return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
}

/**
 * 获取设备信息摘要
 * @returns object
 */
export function getDeviceInfo() {
  return {
    isMobile: isMobileDevice(),
    deviceType: getDeviceType(),
    os: getOperatingSystem(),
    browser: getBrowserType(),
    isTouch: isTouchDevice(),
    isWechat: isWeChatBrowser(),
    isQQ: isQQBrowser(),
    isWeibo: isWeiboBrowser(),
    userAgent: typeof window !== 'undefined' ? navigator.userAgent : '',
    screenSize: typeof window !== 'undefined' ? {
      width: window.innerWidth,
      height: window.innerHeight
    } : null
  };
}
